<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 任务管理ajax类
 * @package  ajax
 * @subpackage  backend
 * @create by allen.qiang
 */

quanli_require('module/extend/app/constant.php');
quanli_require('module/base/common/ebsigAsync.class.php');
quanli_require('module/base/taskHandler.class.php');

class ajax_assignment {

    public function execute(){

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->searchAssignmentList();//任务列表
                    break;

                case 2:
                    $this->editAssignment();//修改、新增任务
                    break;

//                case 3:
//                    $this->deleteAssignment();//删除
//                    break;
            }

        } else {
            $this->toReturn('参数错误');
        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    /**
     * 任务列表
     */
    private function searchAssignmentList(){

        global $G_PGLOB;

        //实例类
        $taskManagerObj = new taskManager($G_PGLOB->DBCA);
       
        $taskObjList=$taskManagerObj->query($_REQUEST);
     

        //页面输出数组
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ( $taskObjList ) {

            foreach ($taskObjList as & $taskObj){

                switch ($taskObj['taskGroupId']) {

                    case 1:
                        $taskGroup = '系统任务';
                        break;

                    case 2:
                        $taskGroup = 'B2C商城';
                        break;

                    case 3:
                        $taskGroup = 'CRM';
                        break;

                    case 4:
                        $taskGroup = '微电汇';
                        break;

                    case 5:
                        $taskGroup = 'xShop';
                        break;
                }

                $result_array['rows'][] = array(
                    'id' => $taskObj['taskId'],
                    'cell' => array(
                        '<a href="javascript:void(0);" onclick="assignment.edit(' . $taskObj['taskId'] . ');">修改</a>
                        &nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" onclick="assignment.content(' . $taskObj['taskId'] . ');">详情</a>',
                        $taskObj['taskName'],
                        $taskGroup,
                        $taskObj['taskFile'],
                        $taskObj['taskScriptName'],
                        $taskObj['taskExecCrontab']
                    )
                );
            }

        }

        echo json_encode($result_array);

    }

    /**
     * 新增或修改任务
     */
    private function editAssignment(){

        global $G_PGLOB;

        if(!isset($_REQUEST['taskGroupId'],$_REQUEST['taskId'],$_REQUEST['taskName'],$_REQUEST['taskFile'],$_REQUEST['taskExecCrontab'])||is_numeric($_REQUEST['taskGroupId'])){
            echo json_encode(array('code'=>400,'message'=>'参数错误'));
        }
		
        $commHandlerObj=new commHandler($G_PGLOB->DBCA);
        //新增或修改任务
        $taskObj=new task();
        if($_REQUEST['taskId'] == 0){
        	$taskObj->taskId=$commHandlerObj->generate_seqno('seqno_task_attr');
            $taskObj->createTime = time();
            $taskObj->creator=$G_PGLOB->currentUserID;
            $taskObj->taskCode = make_uuid(); 
        }else{
            $taskObj->taskId = $_REQUEST['taskId'];
        }
        $taskObj->timeStamp             =time();
        $taskObj->taskGroupId           =$_REQUEST['taskGroupId'];
        $taskObj->taskName              =$_REQUEST['taskName'];
        $taskObj->taskFile              =$_REQUEST['taskFile'];
        $taskObj->remark                =$_REQUEST['remark'];
        $taskObj->taskCycleType        =$_REQUEST['taskExecCrontab'];
        
        //每天发送一次
        if($_REQUEST['taskExecCrontab'] == 1 && !empty($_REQUEST['taskExecTime_1'])){
            $time_arr = explode(':',$_REQUEST['taskExecTime_1']);
            $taskObj->everyDay($time_arr[0],$time_arr[1]);
        }

        //每周发送一次
        if($_REQUEST['taskExecCrontab'] == 2 && !empty($_REQUEST['taskExecTime_1']) && !empty($_REQUEST['taskExecTime_2'])){
            $time_arr = explode(':',$_REQUEST['taskExecTime_1']);
            $taskObj->everyWeek($_REQUEST['taskExecTime_2'],$time_arr[0],$time_arr[1]);
        }

        //每月发送一次
        if($_REQUEST['taskExecCrontab'] == 3 && !empty($_REQUEST['taskExecTime_1']) && !empty($_REQUEST['taskExecTime_3'])){
            $time_arr = explode(':',$_REQUEST['taskExecTime_1']);
            $taskObj->everyMonth($_REQUEST['taskExecTime_3'],$time_arr[0],$time_arr[1]);
        }

        //每间隔分钟执行
        if($_REQUEST['taskExecCrontab'] == 4 && !empty($_REQUEST['taskExecTime_4'])){
            $taskObj->intervalMin($_REQUEST['taskExecTime_4']);
        }

        //每间隔多少小时执行一次
        if($_REQUEST['taskExecCrontab'] == 5 && !empty($_REQUEST['taskExecTime_5'])){
            $taskObj->intervalMin($_REQUEST['taskExecTime_5']);
        }

        //每间隔多少天执行一次
        if($_REQUEST['taskExecCrontab'] == 6 && !empty($_REQUEST['taskExecTime_6'])){
            $taskObj->intervalDay($_REQUEST['taskExecTime_6']);
        }
        $taskManagerObj=new taskManager($G_PGLOB->DBCA);
        $result_array = $taskManagerObj->save($taskObj);


//        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
//        $result_array = $userHandler->editAssignment( $_REQUEST );

        echo json_encode( $result_array );

    }

    /**
     * 删除任务
     */
//    private function deleteAssignment() {
//
//        if (!isset($_REQUEST['p_array']) || empty($_REQUEST['p_array'])) {
//            $this->toReturn('参数错误2');
//        }
//
//        global $G_PGLOB;
//
//        //实例化用户类
//        $userHandler = new userHandler( $G_PGLOB->DBCA );
//
//        //删除任务
//        $assignment_array = explode(',', $_REQUEST['p_array']);
//        $result_array = $userHandler->deleteAssignment( $assignment_array );
//
//        echo json_encode( $result_array );
//
//    }

}